// pages/exam/index/index.js
let app = getApp()
Page({
  data: {
    placeholder: '输入部门关键字查询',
    spinShow: false,
    loadMoreLoad: false,
    loadMoreTip: '暂无数据',
    inputValue:'',
    selectRadio:'r1',
    titleHidden:false,
    contentHidden:true,
    queryParam: {
      pageIndex: 1,
      pageSize: app.globalData.pageSize,
      userId:null,
      deptId:null,
      employeeSelectData:null
    },
    queryPersonParam: {
        keyword: '',
      
      },
      queryEmployeeListParam: {
        deptId:null,
      },
      queryCompanyParam: {
        keyword: '',    
      },
    tableData: [],
    total: 1,
    showsCompany: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    showsEmployee: false, //控制下拉列表的显示隐藏，false隐藏、true显示

    companyData:[],
    selectCompanyDatas: [], //下拉列表的数据
    employeeData:[],
    selectEmployeeDatas: [], //下拉列表的数据

    indexsCompany: 0, //选择的下拉列 表下标,
    indexsEmployee: 0, //选择的下拉列 表下标,

    deptId:null,

  },
  onLoad: function(options) {
    this.setData({
      spinShow: false
    });
    this.getDepartmentList(true);
    // this.search(true)

  },
  onPullDownRefresh() {
      console.log('eeee')
    this.setData({
      spinShow: true
    });
    if (!this.loading) {
      this.setData({
        ['queryParam.pageIndex']: 1
      });
    //   if(this.data.deptId !== null){

    //   }
      this.search(true)
    }
  },
  onReachBottom() {
    if (!this.loading && this.data.queryParam.pageIndex < this.data.total) {
      this.setData({
        loadMoreLoad: true,
        loadMoreTip: '正在加载'
      });
      this.setData({
        ['queryParam.pageIndex']: this.data.queryParam.pageIndex + 1
      });
      this.search(false)
    }
  },
  search: function(override) { 
    let _this = this
    app.formPostToBody('/api/wx/student/exampaper/answer/search/pageList', this.data.queryParam)
      .then(res => {
        _this.setData({
          spinShow: false 
        });
        wx.stopPullDownRefresh()
        if (res.code === 1) {
          const re = res.response
          _this.setData({
            ['queryParam.pageIndex']: re.pageNum,
            tableData: override ? re.list : this.data.tableData.concat(re.list),
            total: re.pages
          });
          if (re.pageNum >= re.pages) {
            this.setData({
              loadMoreLoad: false,
              loadMoreTip: ''
            });
          }
       
        }
      }).catch(e => {
        _this.setData({
          spinShow: false
        });
        app.message(e, 'error')
      })
  },
  // getInput: function (e) {
  //   this.setData({
  //     inputValue: e.detail.value
  //   })
  //   console.log('radio发生change事件，携带value值为：',this.data.selectRadio)
  //   this.setData({
  //       queryCompanyParam: {
  //           keyword: e.detail.value,
          
  //         },
  //   })
  //   if (e.detail.value !== '') {
  //       this.queryCompany();
  //   } else {
  //       this.search(true)
  //   }
  
  // },

  getUserName:function(e){
    let _this = this
    this.setData({

      queryEmployeeListParam: {
        keyword: e.detail.value,
        deptId:this.data.deptId,
        showsEmployee: true,
        showsCompany: false
      },

    })
    this.getEmployeeList(true);
    // this.setData({
    //     showsEmployee: false,
    // })
  },
  
  // 点击下拉显示框
  selectCompanyTaps() {

    this.setData({
      showsCompany: !this.data.showsCompany,
    });

  },
  // 点击下拉显示框
  selectEmplpoyeeTaps() {
    this.setData({
        employeeData:this.data.employeeData,
        // selectEmployeeDatas:this.data.selectEmployeeDatas,
    showsEmployee: !this.data.showsEmployee,
    });
  },
  // 点击下拉列表
  optionCompanyTaps(e) {
    this.setData({
        employeeSelectData: '',
        tableData:[]
    })
    let Indexs = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    console.log(Indexs)
    this.setData({
      indexsCompany: Indexs,
      showsCompany: !this.data.showsCompany,
      showsEmployee: false,
      queryEmployeeListParam:{
        deptId:this.data.selectCompanyDatas[Indexs].id,
      },
      deptId :this.data.selectCompanyDatas[Indexs].id,
      queryParam: {
        pageIndex: 1,
        pageSize: app.globalData.pageSize,
        userId:null,
        deptId:this.data.selectCompanyDatas[Indexs].id,
      },
      contentHidden:false,
      titleHidden:true,
    });

    this.getEmployeeList(false)

  },
  // 点击下拉列表
  optionEmployeeTaps(e) {
    let Indexs = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    console.log(Indexs)
    this.setData({
      indexsEmployee: Indexs,
      showsCompany: false,
      showsEmployee: false,
      employeeSelectData:this.data.employeeData[Indexs],

      queryParam: {
        pageIndex: 1,
        pageSize: app.globalData.pageSize,
        userId:this.data.selectEmployeeDatas[Indexs].id,
        deptId:this.data.deptId,
      },
    });

  },
  radioChange(e) {
 
    this.setData({
        selectRadio:e.detail.value
    })

    if (this.data.selectRadio == 'r1') {
        this.setData({
            placeholder: '请输入员工名称'
        })
    } else {
        this.setData({
            placeholder: '请输入部门名称'
        })
    }
  },

  queryPerson: function(override) {
    let _this = this
    app.formGetToBody('/api/wx/student/exampaper/answer/search/employee', this.data.queryPersonParam)
      .then(res => {
        _this.setData({
          spinShow: false,
          tableData:[],
        });
        if (res.code === 1) {
            if(res.response.length>0){
                const re = res.response;
             
                for(const i in re){
                    _this.setData({
                        queryParam: {
                            pageIndex: 1,
                            pageSize: app.globalData.pageSize,
                            userId:re[i].id,
                            deptId:null,
                          },
                      });
                      this.search(false); 
                }
      
            }
        }
      }).catch(e => {
        _this.setData({
          spinShow: false
        });
        app.message(e, 'error')
      })
  },
  queryCompany: function(override) {
    let _this = this
    app.formGetToBody('/api/wx/student/exampaper/answer/search/dept', this.data.queryCompanyParam)
      .then(res => {
        _this.setData({
          spinShow: false,
          tableData:[],
        });
        if (res.code === 1) {
            if(res.response.length>0){
          const re = res.response
    
          for(const i in re){
            _this.setData({
                queryParam: {
                    pageIndex: 1,
                pageSize: app.globalData.pageSize,
                userId:null,
                deptId:re[i].id,
                  },
              });
              this.search(false); 
        }
        }
        }
      }).catch(e => {
        _this.setData({
          spinShow: false
        });
        app.message(e, 'error')
      })
  },

  getDepartmentList: function(override) {
    let _this = this
    app.formGetToBody('/api/wx/student/exampaper/answer/rank/dept', null)
      .then(res => {
 
        if (res.code === 1) {
            if(res.response.length>0){
          const re = res.response
          
          for(const i in re){
            
            _this.data.selectCompanyDatas.push(re[i])
            _this.data.companyData.push(re[i].name)
          }
          console.log('this.data.companyData:', this.data.companyData)
          this.setData({
            companyData:this.data.companyData,
            selectCompanyDatas:this.data.selectCompanyDatas,

          })
        }
        }
      }).catch(e => {
 
        app.message(e, 'error')
      })
  },
  getEmployeeList: function(override) {
    let _this = this
    app.formGetToBody('/api/wx/student/exampaper/answer/dept/user', this.data.queryEmployeeListParam)
      .then(res => {
        console.log("res.code是"+res.message)
 
        if (res.code === 1) {
            if(res.response.length>0){
          const re = res.response
          this.setData({
            employeeData:[],
            selectEmployeeDatas:[]
          })
          for(const i in re){
            _this.data.employeeData.push(re[i].name),
            _this.data.selectEmployeeDatas.push(re[i])
          }
          this.setData({
            showsEmployee: override ? true : false,
            employeeData:this.data.employeeData,
            selectEmployeeDatas:this.data.selectEmployeeDatas,
          })
   

        }
        }
      }).catch(e => {
        wx.showToast({
            title: '请先选择部门！',
            icon: 'fail',
            duration: 1000,
            mask:true
            })
        // app.message(e, 'error')
      })
  },
  qurey:function(event){
    if(this.data.deptId === null){
        wx.showToast({
            title: '请先选择需要查询的部门或员工！',
            icon: 'none',
            duration: 1000,
            mask:true
            })

            return
    }
    // ['queryParam.pageIndex']: this.data.queryParam.pageIndex + 1
    this.setData({
        ['queryParam.pageIndex']: this.data.queryParam.pageIndex
    });
    this.search(true)
  },
  departBlur:function(){
    this.setData({
        showsCompany: false
    })
  },
  keepshowEmpList:function(){
    this.setData({
        showsEmployee: true
    })
  },
  empBlur:function(){
    this.setData({
        showsEmployee: false
    })
  },
  abc:function() {
    this.setData({
        showsCompany: true
    })
  },
  ABC:function(){
    this.setData({
        showsEmployee: true
    })
  },
  getDetail:function(event){
    // console.log("event是")
    // console.log(event)
    // url: '../../pages/exam/read/index?id='+ event.currentTarget.dataset.id + '&ana=' + event.currentTarget.dataset.ana + '&ans=' + event.currentTarget.dataset.ans,
    wx.navigateTo({
      url: '../../pages/exam/read/index?id='+ event.currentTarget.dataset.id,
    })
}
})